package com.study.bigwork.wrongQuestion.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.study.bigwork.entity.Homework;
import com.study.bigwork.entity.WrongQuestion;


@Repository
public class WrongQuestionDao {
	@Resource
	private SessionFactory sessionFactory;
	
	//新增错题到数据库
	public boolean addWrongQuestion(WrongQuestion wrongQuestion) {
		Session session = this.sessionFactory.getCurrentSession();
		Gson gson = new Gson();
		if(wrongQuestion.getHomework_image()!=null) {
			String homework_image1=gson.toJson(wrongQuestion.getHomework_image());
			wrongQuestion.setHomework_image1(homework_image1);
		}
		if(wrongQuestion.getResult_image()!=null) {
			String result_image1 = gson.toJson(wrongQuestion.getResult_image());
			wrongQuestion.setResult_image1(result_image1);
		}
		if(wrongQuestion.getResult_image_teacher()!=null){
			String result_image_teacher1=gson.toJson(wrongQuestion.getResult_image_teacher());
			wrongQuestion.setResult_image_teacher1(result_image_teacher1);
		}
		session.save(wrongQuestion);
		return true;
	}
	//获取错误信息
	public List<WrongQuestion> getWrongQuestions(String questionType,String id){
		Session session=sessionFactory.getCurrentSession();
		Query query = session.createQuery("from WrongQuestion where question_Type=? and user_id=?");
		query.setParameter(0,questionType);
		query.setParameter(1,Integer.parseInt(id));
		System.out.println(questionType+"   "+id);
		List<WrongQuestion> wrongQuestions=query.getResultList();
		for(int i=0;i<wrongQuestions.size();i++) {
			WrongQuestion wrongQuestion=wrongQuestions.get(i);
			wrongQuestion.setResult_image();
			wrongQuestion.setResult_image_teacher();
			wrongQuestion.setHomework_image();
		}
		if(wrongQuestions!=null) {
			return wrongQuestions;
		}
		return null;
	}
}
